package com.puppy.controller;//package com.puppy.controller;

import com.puppy.pojo.Emp;
import com.puppy.pojo.EmpQueryParam;
import com.puppy.pojo.Result;
import com.puppy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;
    /*分页查询*/
//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "5") Integer pageSize){ //和前端名称一致
//        log.info("分页查询，参数：page={},pageSize={}",page,pageSize);
//        return Result.success(empService.page(page,pageSize));
//    }
//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")
//    Integer pageSize , String name, Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){ //和前端名称一致
//        log.info("分页查询，参数：page={},pageSize={},name={},gender={}",page,pageSize,name,gender);
//        return Result.success(empService.page(page,pageSize,name,gender,begin,end));
//    }

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {//和前端名称一致
        log.info("分页查询，参数：{}", empQueryParam);
        return Result.success(empService.page(empQueryParam));
    }

    @PostMapping
    public Result add(@RequestBody Emp emp) { //@RequestBody接受的请求体-json格式
        log.info("新增员工，员工信息：{}", emp);
        empService.save(emp);
        return Result.success();
    }
//    @DeleteMapping
//    public Result delete(Integer[] ids){
//        log.info("删除员工，id：{}", Arrays.toString(ids));
//        return Result.success();
//    }

    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工，id：{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    //@PathVariable注解表示获取路径中的变量给形参，{id}是路径变量，id是参数名
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息，id：{}", id);
        return Result.success(empService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息，员工信息：{}", emp);
        empService.update(emp);
        return Result.success();
    }
}
